package com.omar.labs.tamtamy.utils;

import java.util.ArrayList;

public class XMLUtility {

	public static String getNodeValue(String XML, String nodeName){
		String tokenValue = null;
		int start = XML.indexOf("<"+nodeName+">") + ("<"+nodeName+">").length();
		int stop = XML.indexOf("</"+nodeName+">");
		if(start>stop || stop<0) return "";
		try{
		  tokenValue = XML.substring(start, stop);
		}catch (Exception ex){
		  ex.printStackTrace();	
		}		
		return tokenValue;
	}
	
	public static String getNodeValueWithAttribute(String XML, String nodeName, String attribute){
		String tokenValue = null;
		int start = XML.indexOf("<"+nodeName+" " + attribute + ">") + ("<"+nodeName+" " + attribute + ">").length();
		int stop = XML.indexOf("</"+nodeName+">");
		try{
		  tokenValue = XML.substring(start, stop);
		}catch(Exception ex){
			
		}
		return tokenValue;
	}
	
	public static ArrayList<String> getNodesValue(String xml, String nodeName){
		String[] nodesValue = null;
		ArrayList<String> v = new ArrayList<String>();
		while(xml.indexOf("<"+nodeName+">")!=-1){
		  int start = xml.indexOf("<"+nodeName+">") + ("<"+nodeName+">").length();
		  int stop = xml.indexOf("</"+nodeName+">");
		  String tmp = xml.substring(start, stop);		 
		  v.add(tmp);
		  xml = xml.substring((stop + ("</"+nodeName+">").length()));
		}			
		return v;
	}
}
